package boot

import (
	"fmt"
	"gitee.com/gopher2011/bookman/app/dao"
	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
	"log"
)

func init() {
	v := viper.New()
	v.SetConfigFile("./config.toml")
	err := v.ReadInConfig()
	if err != nil {
		log.Println(err)
		panic(fmt.Errorf("Fatal error config file : %s \n", err))
	}
	v.WatchConfig()
	v.OnConfigChange(func(in fsnotify.Event) {
		log.Println("config file changed:", in.Name)
		if err = v.Unmarshal(&dao.Cfg); err != nil {
			log.Println(err)
		}
	})
	if err = v.Unmarshal(&dao.Cfg); err != nil {
		log.Println(err)
	}
}
